package cn.edu.zafu.bigdata.yt.wjcz2;

import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

public class StudentManager_yt {
    private static final String FILE_NAME = "students.dat";
    private HashMap<String, Student_yt> studentMap;

    public StudentManager_yt() {
        loadStudents();
    }

    // 加载学生信息
    @SuppressWarnings("unchecked")
    private void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentMap = (HashMap<String, Student_yt>) ois.readObject();
        } catch (FileNotFoundException e) {
            studentMap = new HashMap<>();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("Error loading student data.");
            studentMap = new HashMap<>();
        }
    }

    // 保存学生信息
    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentMap);
        } catch (IOException e) {
            System.out.println("Error saving student data.");
        }
    }

    // 添加学生信息
    public void addStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter student ID: ");
        String id = scanner.nextLine();
        if (studentMap.containsKey(id)) {
            System.out.println("Student ID already exists. Try again.");
            return;
        }
        System.out.print("Enter student name: ");
        String name = scanner.nextLine();
        System.out.print("Enter student phone: ");
        String phone = scanner.nextLine();

        Student_yt student = new Student_yt(id, name, phone);
        studentMap.put(id, student);
        saveStudents();
        System.out.println("Student added successfully.");
    }

    // 检索学生信息
    public void searchStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter student ID to search (or type 'QUIT' to go back): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student_yt student = studentMap.get(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("Student not found.");
            }
        }
    }

    // 更新学生信息
    public void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter student ID to update (or type 'QUIT' to go back): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            Student_yt student = studentMap.get(id);
            if (student != null) {
                System.out.print("Enter new name: ");
                String name = scanner.nextLine();
                System.out.print("Enter new phone: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                saveStudents();
                System.out.println("Student updated successfully.");
            } else {
                System.out.println("Student not found.");
            }
        }
    }

    // 删除学生信息
    public void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter student ID to delete (or type 'QUIT' to go back): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            if (studentMap.containsKey(id)) {
                studentMap.remove(id);
                saveStudents();
                System.out.println("Student deleted successfully.");
            } else {
                System.out.println("Student not found.");
            }
        }
    }
}
